const validatePhone = (rule, value, callback) => {
  let patter = /^((\+?86)|(\(\+86\)))?(13[0123456789][0-9]{8}|15[0123456789][0-9]{8}|17[0123456789][0-9]{8}|18[0123456789][0-9]{8}|147[0-9]{8}|1349[0-9]{7})$/;
  if (!value) {
    callback('请输入正确格式的手机号或者座机！');
  } else if (!patter.test(value)) {
    return callback(new Error('请输入正确格式的手机号或者座机！'));
  } else {
    callback(); //必须有此项回调，否则验证会一直不通过
  }
};
const validateMobilePhone = (rule, value, callback) => {
  let patter = new RegExp('^1[0-9]{10}$'); //简单验证11位手机号
  if (!value) {
    callback('请输入正确格式的手机号！');
  } else if (!patter.test(value)) {
    return callback(new Error('请输入正确格式的手机号！'));
  } else {
    callback(); //必须有此项回调，否则验证会一直不通过
  }
};
const validateCard = (rule, value, callback) => {
  let patter = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/; //简单验证身份证号
  if (value === '') {
    callback();
  } else if (!patter.test(value)) {
    return callback(new Error('身份证输入不合法！'));
  } else {
    callback(); //必须有此项回调，否则验证会一直不通过
  }
};
const validateEmail = (rule, value, callback) => {
  let patter = new RegExp('^[a-z0-9A-Z]+[- | a-z0-9A-Z . _]+@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-z]{2,}$'); //简单验证邮箱
  if (value === '') {
    callback();
  } else if (!patter.test(value)) {
    return callback(new Error('邮箱格式不正确，请重新输入！'));
  } else {
    callback(); //必须有此项回调，否则验证会一直不通过
  }
};
const validateAngle = (rule, value, callback) => {
  let patter = /^(-)?\d{1,3}(\.\d{1,15})?$/;
  if (value === '') {
    callback('请输入角度');
  } else if (!patter.test(value)) {
    return callback(new Error('请输入0到360度，小数最多15位！'));
  } else if (~~value > 360) {
    return callback(new Error('请输入0到360度，小数最多15位！'));
  } else if (~~value < 0) {
    return callback(new Error('请输入0到360度，小数最多15位！'));
  } else {
    callback(); //必须有此项回调，否则验证会一直不通过
  }
};
const validateNum = (rule, value, callback) => {
  let patter = /^\d{1,10}$|^\d{1,10}[.]\d{1,10}$/;
  if (!value) {
    if (value == 0) {
      callback();
    } else {
      callback('请输入10位以下非负数，小数最多10位！');
    }
  } else if (!patter.test(value)) {
    return callback(new Error('请输入10位以下非负数，小数最多10位！'));
  } else {
    callback(); //必须有此项回调，否则验证会一直不通过
  }
};
const validateInput20 = (rule, value, callback) => {
  if (value === '') {
    callback();
  } else if (String(value).length > 20) {
    return callback(new Error('最长20个字符！'));
  } else {
    callback();
  }
};
const validateRequiredInput20 = (rule, value, callback) => {
  if (!value) {
    return callback(new Error('必填项不能为空'));
  } else if (String(value).length > 20) {
    return callback(new Error('最长20个字符！'));
  } else {
    callback();
  }
};
const validateRequiredInput50 = (rule, value, callback) => {
  if (!value) {
    return callback(new Error('必填项不能为空'));
  } else if (String(value).length > 50) {
    return callback(new Error('最长50个字符！'));
  } else {
    callback();
  }
};
const validateCoordinate = (rule, value, callback) => {
  if (!value) {
    return callback(new Error('请输入经纬度'));
  }
  let coor = value.split(',');
  if (coor.length !== 2) {
    return callback(new Error('经(0-180°)纬(0-90°)，小数20位，英文逗号隔开'));
  }
  coor[0] = Number(coor[0]);
  coor[1] = Number(coor[1]);
  let patter1 = /^(-)?\d{1,3}(\.\d{1,20})?$/;
  let patter2 = /^(-)?\d{1,2}(\.\d{1,20})?$/;
  if (!patter1.test(~~coor[0])) {
    return callback(new Error('经(0-180°)纬(0-90°)，小数20位，英文逗号隔开'));
  } else if (~~coor[0] > 180) {
    return callback(new Error('经(0-180°)纬(0-90°)，小数20位，用英文逗号隔开'));
  } else if (!patter2.test(~~coor[1])) {
    return callback(new Error('经(0-180°)纬(0-90°)，小数20位，用英文逗号隔开'));
  } else if (~~coor[1] > 90) {
    return callback(new Error('经(0-180°)纬(0-90°)，小数20位，用英文逗号隔开'));
  } else if (~~coor[0] < 0 || ~~coor[0] > 180) {
    return callback(new Error('经(0-180°)纬(0-90°)，小数20位，用英文逗号隔开'));
  } else if (~~coor[1] < 0 || ~~coor[1] > 90) {
    return callback(new Error('经(0-180°)纬(0-90°)，小数20位，用英文逗号隔开'));
  } else {
    callback(); //必须有此项回调，否则验证会一直不通过
  }
};
const validatePort = (rule, value, callback) => {
  let patter = /^[1-9]\d*$/;
  if (value === '0') {
    callback();
  } else if (!patter.test(value)) {
    return callback(new Error('请输入0-65535之间的整数'));
  } else if (value > 65535) {
    return callback(new Error('请输入0-65535之间的整数'));
  } else {
    callback(); //必须有此项回调，否则验证会一直不通过
  }
};
const validateIP = (rule, value, callback) => {
  let patter = /^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$/;
  if (!value) {
    return callback(new Error('必填项不能为空'));
  } else if (!patter.test(value)) {
    return callback(new Error('请输入正确的IP格式'));
  } else {
    callback(); //必须有此项回调，否则验证会一直不通过
  }
};
const validatePostCode = (rule, value, callback) => {
  let patter = /^\d{6}$/;
  if (!patter.test(value)) {
    return callback(new Error('请输入正确的邮政编码'));
  } else {
    callback(); //必须有此项回调，否则验证会一直不通过
  }
};
const validateTelphone = (rule, value, callback) => {
  let patter = /^([0-9]{3,4}-)?[0-9]{7,8}$/;
  if (value === '0') {
    callback();
  } else if (!patter.test(value)) {
    return callback(new Error('请输入正确的座机号码'));
  } else {
    callback(); //必须有此项回调，否则验证会一直不通过
  }
};
const validateFax = (rule, value, callback) => {
  let patter = /^(\d{3,4}-)?\d{7,8}$/;
  if (value === '0') {
    callback();
  } else if (!patter.test(value)) {
    return callback(new Error('请输入正确的传真号'));
  } else {
    callback(); //必须有此项回调，否则验证会一直不通过
  }
};
//校验密码
const isValidPassword = (password, minLength = 8) => {
  const specialChars = '!"#$%&\'()*+,-./:;<=>?@$$^_`{|}~';
  const regexStr = `^(?:(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)|(?=.*[a-z])(?=.*[A-Z])(?=.*[${specialChars}])|(?=.*[a-z])(?=.*\\d)(?=.*[${specialChars}])|(?=.*[A-Z])(?=.*\\d)(?=.*[${specialChars}])).{${minLength},}$`;
  const regex = new RegExp(regexStr);
  return regex.test(password);
};
//获取DOM的尺寸
function getFullDimensions(el) {
  const style = window.getComputedStyle(el);
  const contentHeight = el.clientHeight; // content + padding
  const padding = {
    top: parseInt(style.paddingTop),
    right: parseInt(style.paddingRight),
    bottom: parseInt(style.paddingBottom),
    left: parseInt(style.paddingLeft)
  };
  const margin = {
    top: parseInt(style.marginTop),
    right: parseInt(style.marginRight),
    bottom: parseInt(style.marginBottom),
    left: parseInt(style.marginLeft)
  };
  const border = {
    top: parseInt(style.borderTopWidth),
    right: parseInt(style.borderRightWidth),
    bottom: parseInt(style.borderBottomWidth),
    left: parseInt(style.borderLeftWidth)
  };
  const totalHeight = margin.top + border.top + padding.top + el.offsetHeight + padding.bottom + border.bottom + margin.bottom;
  return {
    contentHeight,
    padding,
    margin,
    border,
    totalHeight
  };
}
export default {
  getFullDimensions,
  isValidPassword,
  //必填
  input: {
    required: true,
    message: localStorage.getItem('language') === 'true' ? '请输入' : '修改',
    trigger: 'blur'
  },
  //常规20个字符输入
  input20: {
    validator: validateInput20,
    trigger: 'blur'
  },
  //常规20个字符输入-必填
  requiredInput20: {
    required: true,
    validator: validateRequiredInput20,
    trigger: 'blur'
  },
  //常规50个字符输入-必填
  requiredInput50: {
    required: true,
    validator: validateRequiredInput50,
    trigger: 'blur'
  },
  //必选
  select: {
    required: true,
    message: localStorage.getItem('language') === 'true' ? '请选择' : '修改',
    trigger: 'change'
  },
  //角度-整数最多360，小数15位，不为负数
  angle: {
    required: true,
    validator: validateAngle,
    trigger: 'blur'
  },
  //含小数的数字，小数10位，不为负数
  num: {
    required: true,
    validator: validateNum,
    trigger: 'blur'
  },
  //验证经纬度
  coordinate: {
    required: true,
    validator: validateCoordinate,
    trigger: 'blur'
  },
  //端口号
  port: {
    required: true,
    validator: validatePort,
    trigger: 'blur'
  },
  //IP
  deviceIP: {
    required: true,
    validator: validateIP,
    trigger: 'blur'
  },
  //手机或者座机
  phone: {
    required: true,
    validator: validatePhone,
    trigger: 'blur'
  },
  //手机号
  mobilePhone: {
    required: true,
    validator: validateMobilePhone,
    trigger: 'blur'
  },
  //座机
  telPhone: {
    validator: validateTelphone,
    trigger: 'blur'
  },
  //邮政编码
  postCode: {
    required: true,
    validator: validatePostCode,
    trigger: 'blur'
  },
  //传真
  fax: {
    validator: validateFax,
    trigger: 'blur'
  },
  //身份证号-格式校验
  cardNum: {
    validator: validateCard,
    trigger: 'blur'
  },
  //邮箱-格式校验
  email: {
    validator: validateEmail,
    trigger: 'blur'
  }
};
